<template>
  <div class="form">
    <el-form label-position="top" label-width="80px" :model="menuForm">
      <el-form-item label="名称">
        <el-input v-model="menuForm.name"></el-input>
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="menuForm.remark"></el-input>
      </el-form-item>

      <el-form-item label="配置">
        <el-input
          type="textarea"
          autosize
          placeholder="请输入内容"
          v-model="menuForm.formInfo">
        </el-input>
      </el-form-item>

      <div style="text-align: right;">
        <el-form-item>
          <el-button type="primary" @click="onAdd">保存</el-button>
          <el-button @click="onClose">取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import api from '@/api'

export default {
  name: 'curd-form-add',
  components: {
  },
  props: {
  },
  data () {
    return {
      menuForm: {
        formInfo: {}
      },
      randomKey: 0,
      jsonTpl: '{"table":[{"tableName":"<表名>","pkField":{"pkFormName":"id","pkFieldName":"id","pkType":"auto"},"fieldMap":[{"formName":"id","fieldName":"id","javaType":"int","update":false},{"formName":"name","fieldName":"name","javaType":"String"}],"whereMap":[{"formName":"id","fieldName":"id","javaType":"int"}]}],"filter":{"name":"bodyToJsonFilter"}}'
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    onAdd () {
      var param = this.menuForm
      param.parentId = this.parentId
      if (this.menuForm.formInfo) {
        param.formInfo = JSON.stringify(JSON.parse(this.menuForm.formInfo))
      } else {
        param.formInfo = '{}'
      }
      api.sysCurd.sysFormInsert(param).then(res => {
        this.$message({ type: 'info', message: '已添加' })
        this.onClose()
        this.$emit('refresh')
      })
    },
    onClose () {
      this.$emit('closeDrawer')
    },
    init () {
      this.menuForm.formInfo = JSON.stringify(JSON.parse(this.jsonTpl), null, 2)
    }
  }
}
</script>

<style lang="scss">
  .form {
    padding: 10px;
  }
</style>
